#include "Bullet.h"


Bullet::Bullet(QGraphicsScene *sc)
{
    root = QFileInfo(__FILE__).absolutePath();
    speed = 20;
    damage = 200;  // 子弹伤害

    pix = QPixmap(root + "/res/_bullet.png").scaled(18, 9);

    for(int i=0;i<5;i++){
        pix_map.append(QPixmap(root + "/res/smoke_0" + (QString)(i + 1) + ".png"));
    }

    Times = 0;
    scene = sc;
    symbol = false;
    setVisible(false);
    QGraphicsItem::setPos(0,600);
    //EnemySpark = EnemySpark(self.scene);

    enemyspark = new EnemySpark(scene);
    scene->addItem(this);
}

Bullet::Bullet(const Bullet &rhs)
{

}


QRectF Bullet::boundingRect() const{
    return QRectF(-pix.width()*0.5,-pix.height()*0.5,pix.width()*0.5,pix.height()*0.5);
}
void Bullet::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    int x;
    if(Times>0&&Times<=50){
        x = Times%5;
        painter->drawPixmap(-pix_map[x].width()*0.5,-pix_map[x].height(),pix_map[x]);
    }
    else if(Times==0){
        painter->drawPixmap(-pix.width()*0.5,-pix.height(),pix);
    }
    else {
        setVisible(false);
        Times = 0;
    }
}

void Bullet::CallSmoke(){
    Times = 1;
}

void Bullet::advance(int phase){
    //qDebug()<<"move";
    if (isVisible()){
        //qDebug()<<phase;
        if (QGraphicsItem::mapToScene(0, 0).x() < 0 || QGraphicsItem::mapToScene(0, 0).y() < 0 || QGraphicsItem::mapToScene(0, 0).x() > scene->width() || QGraphicsItem::mapToScene(0, 0).y() > scene->height()){
            setVisible(false);
        }
        else if(Times == 0){
            QGraphicsItem::setPos(QGraphicsItem::mapToScene(speed, 0));
            QGraphicsItem::collidingItems();
        }

        else if (Times <= 50 && Times > 0){
            QGraphicsItem::setPos(QGraphicsItem::mapToScene(0, 0));
        }
    }

}

bool Bullet::isVisible() const
{
    return QGraphicsItem::isVisible();

}

void Bullet::setVisible(bool visible)
{
    QGraphicsItem::setVisible(visible);
}
